import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../../module_common/widgets/skeleton/dapp_list_skeleton.dart';
import '../../../../module_common/widgets/skeleton/dapp_tag_skeleton.dart';
import '../dapp_empty.dart';
import '../dapp_list.dart';
import '../dapp_tag_item.dart';
import '../dapp_title.dart';

class DAppTagListComp extends StatefulWidget {
  bool showAtBottom = false;

  DAppTagListComp({super.key, this.showAtBottom = false});

  @override
  _DAppTagListCompState createState() => _DAppTagListCompState();
}

class _DAppTagListCompState extends State<DAppTagListComp> {
  bool isLoading = true;

  List<int> compList = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6];
  final List<String> tagsLists = ["All", "Gamefi", "Defi", "ETH", "BSC", "Collectibles"];

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  Future<void> fetchData() async {
    // final response = await http.get(Uri.parse('https://api.example.com/data'));
    // if (response.statusCode == 200) {
    //   setState(() {
    //     data = response.body;
    //   });
    // } else {
    //   // 处理请求失败的情况
    // }
    Future.delayed(Duration(seconds: 7), () {
      // 在延迟时间后执行的操作
      setState(() {
        isLoading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      color: Color.fromRGBO(255, 255, 255, 1),
      child: Column(
        children: [
          DAppTitle(
            title: 'Dapps',
            showHotIcon: false,
            showMore: false,
            isLoading: isLoading,
          ),
          if (!isLoading && tagsLists.length > 0)
            Container(
              margin: EdgeInsets.only(left: 20.w, top: 10.w, bottom: 10.w),
              padding: EdgeInsets.only(top: 8.w, bottom: 2.w),
              height: 40.w,
              child: ListView.builder(
                  itemCount: tagsLists.length,
                  scrollDirection: Axis.horizontal,
                  itemBuilder: (BuildContext context, int index) {
                    return DAppTagItem(
                        onClick: () {
                        },
                        title: tagsLists[index],
                        isActive: index == 0);
                  }),
            ),
          if (isLoading)
            Column(
              children: [
                Container(
                  margin: EdgeInsets.only(left: 20.w, top: 10.w, bottom: 10.w),
                  padding: EdgeInsets.only(top: 8.w, bottom: 2.w),
                  height: 40.w,
                  child: ListView.builder(
                      itemCount: 6,
                      physics: NeverScrollableScrollPhysics(),
                      scrollDirection: Axis.horizontal,
                      itemBuilder: (BuildContext context, int index) {
                        return DAppTagSkeleton();
                      }),
                ),
                for (var item in [1, 2, 3]) DAppListSkeleton(),
              ],
            ),
          if (!isLoading && compList.length == 0)
            Container(
              margin: EdgeInsets.only(top: 50.w),
              child: DAppEmpty(title: 'Empty data'),
            ),
          if (!isLoading && compList.length > 0)
            Column(
              children: [
                for (var item in compList)
                  DAppList(
                      name: 'name',
                      image: 'assets/images/dapp/default-avatar.png',
                      onClick: () {
                        print(item);
                      }),
              ],
            ),
          if (widget.showAtBottom && !isLoading)
            Container(
              padding: EdgeInsets.only(top: 27.w, bottom: 27.w),
              child: Align(
                child: Text('Already at the bottom',
                    style: TextStyle(
                      color: Color.fromRGBO(190, 189, 202, 1),
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400,
                    )),
              ),
            ),
          if (!widget.showAtBottom)
            SizedBox(
              height: 50.w,
            )
        ],
      ),
    );
  }
}
